package gameObjects;

import java.awt.Color;
import java.net.URL;
import radar.Radar;
import logic.Coords2D;
import com.jme.image.Texture;
import com.jme.image.Texture.WrapMode;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.scene.shape.Pyramid;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import editor.Grid;
import editor.Menu.OGGETTI;

public class RiseWall {
	private ColorRGBA 	color;
	private Coords2D 	initPoint;
	private Coords2D 	endPoint;
	private String		name;
	//private Box			cube;
	private Pyramid		cube;
	private int			colIndex;
	private int 		cellX;
	private int			cellY;
	public RiseWall(Coords2D initPoint, ColorRGBA color, int colIndex)
	{
		this.color 		 = color;
		this.initPoint 	 = new Coords2D(initPoint);
		this.endPoint 	 = new Coords2D(initPoint.getX()+1, initPoint.getY()+1);
		this.name		 = initPoint.toString(); 
		this.colIndex    = colIndex;
		this.cellX		 = (int)initPoint.getX();
		this.cellY		 = (int)initPoint.getY();
		this.setCube();
	}
	
	public void setInitPoint(Coords2D point)
	{
		this.initPoint=point;
	}
	
	
	public Coords2D getInitPoint()
	{
		return this.initPoint;
	}
	
	public Coords2D getEndPoint()
	{
		return this.endPoint;
	}
	
	public Pyramid getCube()
	{
		return this.cube;
	}
	
	private void setCube()
	{
		//Vector3f start = new Vector3f(initPoint.getX()+0.1f,  -1f, initPoint.getY()+0.1f);
		Vector3f start = new Vector3f(initPoint.getX()+0.5f,  -1f, initPoint.getY()+0.5f);
		//Vector3f end   = new Vector3f( endPoint.getX()-0.1f,  0f,  endPoint.getY()-0.1f);
		
		String[] colPath = {"black","red","green","gray","blue","yellow"};
		
		MaterialState ms   = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
		ColorRGBA ambient  = new ColorRGBA(0.5f, 0.5f, 0.5f,0.5f);
		ColorRGBA emissive = color;
		ColorRGBA diffuse  = color;
		ColorRGBA specular = new ColorRGBA(0.5f, 0.5f, 0.5f,0.5f);
		float shininess    = 32.0f;
		
		ms.setAmbient(ambient);
		ms.setEmissive(emissive);
		ms.setDiffuse(diffuse);
		ms.setSpecular(specular);
		ms.setShininess(shininess);
		
		URL url = TextureMapping.class.getClassLoader().getResource("textures/" + colPath[colIndex] + "_color.jpg");
		Texture texture = TextureManager.loadTexture(url,
  		        Texture.MinificationFilter.Trilinear,
  		        Texture.MagnificationFilter.Bilinear);
		texture.setWrap(WrapMode.MirrorClamp);
		
		TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
	  	ts.setEnabled(true);
	  	ts.setTexture(texture);
	  	
		//cube = new Box(this.name,start,end);
		cube = new Pyramid(name, 0.8f, 1.0f);
		cube.setLocalTranslation(start);
		cube.setCastsShadows(true);
		cube.setDefaultColor(this.color);
		cube.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
		cube.setSolidColor(this.color);
		cube.setRenderState(ms);
		cube.setRenderState(ts);
		cube.setLightCombineMode(LightCombineMode.CombineClosestEnabled);
	}
	
	public boolean update(float tpf)
	{
		if(cube.getLocalTranslation().y<0.8f)
		{
			float v = 5;
			float x = cube.getLocalTranslation().x;
			float y = cube.getLocalTranslation().y + (v * tpf)*2;
			float z = cube.getLocalTranslation().z;
			cube.setLocalTranslation(x, y, z);
		}
		else 
		{
			cube.setLocalTranslation(cube.getLocalTranslation().x, 0.8f, cube.getLocalTranslation().z);
			return true;
		}
		return false;
	}
	
	public int destroy(float tpf)
	{
		float x = cube.getLocalTranslation().x;
		float y = cube.getLocalTranslation().y;
		float z = cube.getLocalTranslation().z;
		
		if(cube.getLocalTranslation().y>0.0f)
		{
			float v = 1.5f;
			y = y - (v * tpf);
			cube.setLocalTranslation(x, y, z);
			return 0;
		}
		else 
		{
			cube.setLocalTranslation(x, -1.0f, z);
			Grid.setCell(cellX,cellY, OGGETTI.EMPTY);
			Radar.getInstance().getPanel().setCell(new Color(200, 0, 0), new Coords2D(cellX, cellY));
			System.out.println("DESTROYED CUBE (" + cube.getName() + ")" + tpf );
			return 1;
		}
	}
}
